package com.leectrl.server;

import com.leectrl.adapter.UDPServerHandler;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.FixedRecvByteBufAllocator;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

public class UdpServer {
    private static final int port = 12357;
    public void run() throws Exception {
        EventLoopGroup  bosssGroup  = new NioEventLoopGroup();// 用来处理已经被接收的连接
        try{
            Bootstrap server =new Bootstrap();//是一个启动NIO服务的辅助启动类
            server.group(bosssGroup )
                    .channel(NioDatagramChannel.class)  // 这里告诉Channel如何接收新的连接
                    .handler(new UDPServerHandler());
            server.option(ChannelOption.SO_BROADCAST,true);
            server.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535));
            ChannelFuture f = server.bind(port).sync();// 绑定端口，开始接收进来的连接
            System.out.println("UDP服务端启动成功...");
            // 监听服务器关闭监听
            f.channel().closeFuture().sync();

        }finally {
            bosssGroup.shutdownGracefully();
        }
    }
}
